<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
       <el-table-column label="数据日期" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.refDate`" :rules="formRules.refDate" class="mb-0px!">
            <el-date-picker
              v-model="row.refDate"
              :readonly="formType == 'detail'"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择"
            />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="用户渠道" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.userSource`" :rules="formRules.userSource" class="mb-0px!">
            <el-select
              :disabled="formType == 'detail'"
              v-model="row.userSource"
              clearable
              filterable
              class="!w-240px"
            >
              <el-option
                v-for="item in userSourceOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="新增用户数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.newUser`" :rules="formRules.newUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.newUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="取消关注用户数量" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.cancelUser`" :rules="formRules.cancelUser" class="mb-0px!">
            <el-input :readonly="formType == 'detail'" v-model="row.cancelUser" placeholder="请输入" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column v-if="formType != 'detail'" align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row v-if="formType != 'detail'" justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加微信用户增减数据</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { WeixinUserAnalysisApi } from '@/api/integration/weixinuseranalysis'

const props = defineProps<{
  analysisId: undefined, // 分析表数据主键（主表的关联字段）
  formType: undefined // 操作类型
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

const userSourceOptions = [
  {
    value: 0,
    label: '其他合计',
  },
  {
    value: 1,
    label: '公众号搜索',
  },
  {
    value: 17,
    label: '名片分享',
  },
  {
    value: 30,
    label: '扫描二维码',
  },
  {
    value: 57,
    label: '文章内账号名称',
  },
  {
    value: 100,
    label: '微信广告',
  },
  {
    value: 149,
    label: '小程序关注',
  },
  {
    value: 161,
    label: '他人转载',
  },
  {
    value: 200,
    label: '视频号',
  },
  {
    value: 201,
    label: '直播',
  },
]

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.analysisId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return;
    }
    try {
      formLoading.value = true
      formData.value = await WeixinUserAnalysisApi.getWeixinUserSummaryListByAnalysisId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    analysisId: undefined,
    refDate: undefined,
    userSource: undefined,
    newUser: undefined,
    cancelUser: undefined
  }
  row.analysisId = props.analysisId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
